<?php
/**
 * @author sql
 * 群发短信
 * 货主给司机发短信
 */

namespace App\Http\Controllers\Owner;

use App\Models\DriverLable;
use App\Models\DriverMailList;
use App\Models\SmsExtensiveTemplate;
use App\Models\SmsLog;
use App\Models\VLable;
use Illuminate\Http\Request;
use App\Http\Controllers\Owner\BaseController;
use Illuminate\Support\Facades\DB;

class SendSmsController extends BaseController
{


    //标签方式发送短信
    public function add()
    {
        return view('owner.mysend.add');
    }

//获取该货主下的所有司机通讯录标签
    public function selectAdd()
    {
        $uid = $this->owner_id;
        return view('owner.mysend.select', compact('uid', $uid));
    }

    //获取该货主下的所有司机通讯录标签 json

    public function getDriverMailList(Request $request)
    {
        $list = DriverLable::query()->where('owner_id', $this->owner_id)->get()->groupBy('label_id')->toArray();
        $new = [];
        if ($list) {
            $count = max(array_keys($list));
            foreach ($list as $key => $item) {

                $newtemp['id'] = $key;
                $newtemp['pid'] = 0;
                $newtemp['title'] = VLable::query()->select('lable_name')->where('id', $key)->first()->lable_name;
                $new[] = $newtemp;

            }
            foreach ($list as $key => $item) {
                foreach ($item as $new_key => $sonItem) {
                    $newtemp2['id'] = ++$count;
                    $newtemp2['pid'] = $sonItem['label_id'];
                    $newtemp2['title'] = empty(DriverMailList::getInfoById($sonItem['driver_mail_id'])) ? '' : DriverMailList::getInfoById($sonItem['driver_mail_id'])['name'];
                    $new[] = $newtemp2;
                }
            }

        }

        echo json_encode($new);
        exit;
    }


    /***
     * 批量发短信
     */
    public function label(Request $request)
    {
        $ids = $request->post('ids');//通讯录标签
        //查询此通讯标签下的所有司机手机号，然后发送短信
        $model = new  DriverLable();
        $phones = $model->getListByLabelId($ids);
        $msg = $request->post('msg');
        $model = new  SmsExtensiveTemplate();
        $smsModel = new SmsLog();
        $uid = $this->owner_id;
        $sendsource = 2;//1.admin,2货主pc端群发短信
        $response = $model->sendBatch($phones, $msg);//内容相同
        //存入发送日志
        if ($response['code'] != '00000') {
            return $this->error('请求运营商接口失败');
        }
        $all_msg = $response['result'];


        foreach ($all_msg as $i) {
            $smsModel->addLog($i['phone'], $msg, $i['status'], $i['desc'], 1, $uid, $sendsource);
        }
        return $this->success('发送成功');

    }

    /**发送记录
     * @param Request $request
     */
    public function data(Request $request)
    {

        $ownerId = $this->owner_id;
        //  DB::enableQueryLog();
        $model = SmsLog::query()->select([
            'sms_log.id',
            'sms_log.phone', 'v_drivers_mail_list.car_no',
            'sms_log.msg', 'sms_log.desc', 'sms_log.created_at'
        ])->where('sms_log.send_uid', $ownerId)->where('sms_log.send_source', 2);

        if ($request->get('phone')) {
            $model = $model->where('sms_log.phone', 'like', '%' . $request->get('phone') . '%');
        }
        if ($request->get('car_no')) {
            $model = $model->where('v_drivers_mail_list.car_no', 'like', '%' . $request->get('car_no') . '%');
        }
        $model->where('sms_log.send_uid', $ownerId);
        $model->where('sms_log.send_source', 2);
        $model->leftJoin('v_drivers_mail_list', 'v_drivers_mail_list.phone', '=', 'sms_log.phone');
        $res = $model->orderBy('sms_log.id', 'desc')->distinct()
            ->paginate($request->get('limit', 30))
            ->toArray();
        //var_dump(DB::getQueryLog());
        //exit;
        return $this->table($res['data'], $res['total']);

    }

    /**
     * @author 发送日志
     */
    public function log()
    {
        return view('owner.mysend.index');
    }

}
